import Vue from 'vue'
import Router from 'vue-router'
import Main from './views/Main.vue'
import Home from './views/Home.vue'
import SuperUser from './views/SuperUser.vue'
import SystemCon from './views/SystemCon.vue'
import SystemMoni from './views/SystemMoni.vue'
import test from './components/test.vue'
import Flowstaterts from './components/flowstaterts.vue'
// import Personal from './components/Personal.vue'
Vue.use(Router)


const router = new Router({
  mode: 'hash',
  routes: [
    {
      path: '/',
      redirect: '/login',
    },
    {
      path: '/main',
      name: 'main',
      component: Main
    },
    {
      path: '/home',
      name: 'home',
      component: Home,
      meta: { requiresAuth: true },
    },
    {
      path: '/superuser',
      name: 'superuser',
      component: SuperUser,
      meta: { requiresAuth: true },
    },
    
    {
      path: '/systemMoni',
      name: 'systemMoni',
      component: SystemMoni,
      meta: { requiresAuth: true },
    },
    {
      path: '/systemCon',
      name: 'systemCon',
      component: SystemCon,
      meta: { requiresAuth: true },
    },
    {
      path: '/test',
      name: 'test',
      component: test
    },
    {
      path: '/flowstaterts',
      name: 'flowstaterts',
      component: Flowstaterts
    },
    {
      path: '/domain',
      name: 'domain',
      component: () => import(/* webpackChunkName: "domain" */ './views/Domain.vue')
    },
    {
      path: '/weixin_admin',
      name: 'weixin_admin',
      component: () => import(/* webpackChunkName: "domain" */ './components/weixin_admin.vue')
    },
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import(/* webpackChunkName: "about" */ './views/About.vue')
    },
    {
      path: '/jamenList',
      name: 'jamenList',
      component: () => import('./components/jamenList.vue')
    },
    {
      path: '/swagger',
      name: 'swagger',
      component: () => import('./components/swagger.vue')
    },
    {
      path: '/onlineUser',
      name: 'onlineUser',
      component: () => import('./components/onlineUser.vue')
    },
    {
      path: '/cpu',
      name: 'cpu',
      component: () => import('./components/cpu.vue')
    },
    {
      path: '/user',
      name: 'user',
      component: () => import( './components/user.vue')
    },
    {
      path: '/soft_manage',
      name: 'soft_manage',
      component: () => import('./components/soft_manage.vue')
    },
    {
      path: '/department',
      name: 'department',
      component: () => import('./components/department.vue')
    },
    
  ]
});

// 动态加载4.4/5.0登录页
if(router.options && router.options.routes){
  // 去重加载路由
  let flag = false
  let depcaluserAdminToken=""
  let temp=(window.location.href).replace("?","?&").split("&")
  temp=temp?temp:[]
  temp.forEach((e)=>{  if(e.indexOf('adminToken=')===0) depcaluserAdminToken=e.replace('adminToken=',"")})
  if(depcaluserAdminToken){
    document.cookie="adminToken="+depcaluserAdminToken+";path=/";
  }



  for(let i = 0 ; i < router.options.routes.length; i++){
    if(router.options.routes[i].name == 'login'){
      flag = true;
      break;
    }
  }
  if(!flag){
    if(staticPath){
      router.addRoutes([{
        path: '/login',
        name: 'login',
        component: () => import(/* webpackChunkName: "login" */ './views/Login5.vue')
      }])
    }else{
      router.addRoutes([{
        path: '/login',
        name: 'login',
        component: () => import(/* webpackChunkName: "login" */ './views/Login.vue')
      }])
    }
  }
}

router.beforeEach((to, from, next) => {
  if(to.path=='/login') {
    next()
  }else {
    if(to.path=='/home') {
      if(window.sessionStorage.getItem("vuex")){
        let currentUser =  JSON.parse(window.sessionStorage.getItem("vuex")).currentUser;
        if (currentUser && currentUser.isOpenSecurity && currentUser.departments.length > 0 && currentUser.domains.length > 0) {
          //当前是部门及的用户，是不允许跳转到home主页的
          alert("当前登陆用户属于部门级用户，没有权限访问主页")
          // next(from.path);
        }else{
          next();
        }
      }else{
        next();
      }
      // 判断当前的是否为域管理员
      if(sessionStorage.getItem('onlyDomainUser')){
        if(to.meta.requiresAuth){
          next('/main')
        }else{
          next()
        }
      }
    }else{
      next();
    }
  }
})
export default router;